package com.trailwalker.traceability.service;

import com.trailwalker.JavaUtils.log.LogUtils;
import com.trailwalker.traceability.TraceabilitySystemApplication;
import com.trailwalker.traceability.entitiy.body.*;
import com.trailwalker.traceability.net.client.RemoteDataSource;
import com.trailwalker.traceability.net.result.ApiResult;
import com.trailwalker.traceability.net.result.ResponseBodyCode;
import com.trailwalker.traceability.util.JfxUtils;
import com.trailwalker.traceability.widget.dialog.JfxLoadingForm;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

import java.util.ArrayList;

/**
 * @author Trailwalker
 * @create 2021-11-14 14:17
 */
public class ApiDataService {

    private static final String TAG = ApiDataService.class.getSimpleName();

    private static ApiDataService sApiDataService;
    private ArrayList<Disposable> mDisposables;

    private ApiDataService() {
        mDisposables = new ArrayList<>();
        LogUtils.log(TAG, "get a new ApiCreateObservableInstance");
    }

    public static ApiDataService getInstance() {
        if (sApiDataService == null) {
            sApiDataService = new ApiDataService();
        }
        return sApiDataService;
    }

    /**
     * 注册
     *
     * @param application
     * @param accountRequestBody
     * @param consumer
     */
    public void register(TraceabilitySystemApplication application, AccountRequestBody accountRequestBody, Consumer<String> consumer) {
        JfxLoadingForm jfxLoadingForm = JfxUtils.getLoadingForm(application.getStage(), "正在注册...");
        jfxLoadingForm.show();
        Disposable disposable = RemoteDataSource.getInstance(application.getGlobalData()).register(accountRequestBody).subscribe(new Consumer<ApiResult<String>>() {
            @Override
            public void accept(ApiResult<String> stringApiResult) throws Exception {
                jfxLoadingForm.close();
                if (stringApiResult.getResult() == 200) {
                    if (consumer != null) {
                        consumer.accept(stringApiResult.getData());
                    }
                } else {
                    JfxUtils.showLongToast(application.getStage(), stringApiResult.getMsg());
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                throwable.printStackTrace();
                LogUtils.logE(throwable);
                jfxLoadingForm.close();
                JfxUtils.showLongToast(application.getStage(), throwable.getMessage());
            }
        });
        mDisposables.add(disposable);
    }

    /**
     * 登录
     *
     * @param application
     * @param accountRequestBody
     * @param consumer
     */
    public void login(TraceabilitySystemApplication application, AccountRequestBody accountRequestBody, Consumer<LoginResponseBody> consumer) {
        JfxLoadingForm jfxLoadingForm = JfxUtils.getLoadingForm(application.getStage(), "登录中...");
        jfxLoadingForm.show();
        Disposable disposable = RemoteDataSource.getInstance(application.getGlobalData()).login(accountRequestBody).subscribe(new Consumer<ApiResult<LoginResponseBody>>() {
            @Override
            public void accept(ApiResult<LoginResponseBody> apiResult) throws Exception {
                jfxLoadingForm.close();
                if (apiResult.getResult() == 200) {
                    //登录成功
                    if (consumer != null) {
                        consumer.accept(apiResult.getData());
                    }
                } else {
                    if (apiResult.getMsg() != null && apiResult.getMsg().contains("密码错误")) {
                        JfxUtils.showPasswordErrorDialog(application.getStage());
                    } else {
                        JfxUtils.showLongToast(application.getStage(), apiResult.getMsg());
                    }
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                throwable.printStackTrace();
                LogUtils.logE(throwable);
                jfxLoadingForm.close();
                JfxUtils.showLongToast(application.getStage(), throwable.getMessage());
            }
        });
        mDisposables.add(disposable);
    }

    /**
     * 获取工人列表
     *
     * @param application
     * @param consumer
     */
    public void getWorkerList(TraceabilitySystemApplication application, Consumer<WorkerListResponseBody> consumer) {
        Disposable disposable = RemoteDataSource.getInstance(application.getGlobalData()).getWorkerList().subscribe(new Consumer<ApiResult<WorkerListResponseBody>>() {
            @Override
            public void accept(ApiResult<WorkerListResponseBody> workerListResponseBodyApiResult) throws Exception {
                if (workerListResponseBodyApiResult.getResult() == 200) {
                    if (consumer != null) {
                        consumer.accept(workerListResponseBodyApiResult.getData());
                    }
                } else {
                    JfxUtils.showLongToast(application.getStage(), workerListResponseBodyApiResult.getMsg());
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                throwable.printStackTrace();
                LogUtils.logE(throwable);
                JfxUtils.showLongToast(application.getStage(), throwable.getMessage());
            }
        });
        mDisposables.add(disposable);
    }

    /**
     * 录入工序
     *
     * @param application
     * @param processRequestBody
     * @param consumer
     */
    public void trace(TraceabilitySystemApplication application, ProcessRequestBody processRequestBody, Consumer<ProcessResponseBody> consumer) {
        JfxLoadingForm jfxLoadingForm = JfxUtils.getLoadingForm(application.getStage(), "正在提交工序...");
        jfxLoadingForm.show();
        Disposable disposable = RemoteDataSource.getInstance(application.getGlobalData()).trace(processRequestBody).subscribe(new Consumer<ApiResult<ProcessResponseBody>>() {
            @Override
            public void accept(ApiResult<ProcessResponseBody> processResponseBodyApiResult) throws Exception {
                jfxLoadingForm.close();
                int result = processResponseBodyApiResult.getResult();
                if (result == ResponseBodyCode.SUCCESS) {
                    JfxUtils.showShortToast(application.getStage(), "提交成功");
                    if (consumer != null) {
                        consumer.accept(processResponseBodyApiResult.getData());
                    }
                } else if (result == ResponseBodyCode.NOT_FOUND_PRO_STEP) {
                    //未查询到产品工序
                    JfxUtils.showLongToast(application.getStage(), "未查询到产品工序，请联系管理人员");
                } else if (result == ResponseBodyCode.NOT_OWNER_PRO_STEP) {
                    //此产品未录入工序
                    JfxUtils.showLongToast(application.getStage(), "此产品没有这个工序");
                } else if (result == ResponseBodyCode.REPEAT_SCAN) {
                    //重复扫码 需要弹框
                    JfxUtils.showLongToast(application.getStage(), "重复扫码");
                } else if (result == ResponseBodyCode.UNCOMPLETE_PROCESS) {
                    JfxUtils.showLongToast(application.getStage(), "有未完成的工序");
                } else {
                    LogUtils.log(TAG, processResponseBodyApiResult.getMsg());
                    JfxUtils.showLongToast(application.getStage(), processResponseBodyApiResult.getMsg());
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                throwable.printStackTrace();
                LogUtils.logE(throwable);
                jfxLoadingForm.close();
                JfxUtils.showLongToast(application.getStage(), throwable.getMessage());
            }
        });
        mDisposables.add(disposable);
    }

    /**
     * 录入工序
     *
     * @param application
     * @param processRequestBody
     * @param consumer
     */
    public void traceUpload(TraceabilitySystemApplication application, ProcessRequestBody processRequestBody, Consumer<ApiResult<ProcessResponseBody>> consumer) {
        Disposable disposable = RemoteDataSource.getInstance(application.getGlobalData()).traceUpload(processRequestBody).subscribe(new Consumer<ApiResult<ProcessResponseBody>>() {
            @Override
            public void accept(ApiResult<ProcessResponseBody> processResponseBodyApiResult) throws Exception {
                if (consumer != null) {
                    consumer.accept(processResponseBodyApiResult);
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                throwable.printStackTrace();
                LogUtils.logE(throwable);
                if (consumer != null) {
                    ApiResult<ProcessResponseBody> processResponseBodyApiResult = new ApiResult<>();
                    processResponseBodyApiResult.setResult(ResponseBodyCode.ERROR);
                    processResponseBodyApiResult.setMsg(throwable.getMessage());
                    consumer.accept(processResponseBodyApiResult);
                }
            }
        });
        mDisposables.add(disposable);
    }

    /**
     * 工序检测
     *
     * @param application
     * @param processRequestBody
     * @param consumer
     */
    public void check(TraceabilitySystemApplication application, ProcessRequestBody processRequestBody, Consumer<ProcessResponseBody> consumer) {
        JfxLoadingForm jfxLoadingForm = JfxUtils.getLoadingForm(application.getStage(), "正在检测产品ID...");
        jfxLoadingForm.show();
        Disposable disposable = RemoteDataSource.getInstance(application.getGlobalData()).check(processRequestBody).subscribe(new Consumer<ApiResult<ProcessResponseBody>>() {
            @Override
            public void accept(ApiResult<ProcessResponseBody> processResponseBodyApiResult) throws Exception {
                jfxLoadingForm.close();
                int result = processResponseBodyApiResult.getResult();
                if (result == ResponseBodyCode.SUCCESS) {
                    if (consumer != null) {
                        consumer.accept(processResponseBodyApiResult.getData());
                    }
                } else if (result == ResponseBodyCode.NOT_FOUND_PRO_STEP) {
                    //未查询到产品工序 要弹框
                    JfxUtils.showNotFoundProcessDialog(application.getStage());
                } else if (result == ResponseBodyCode.NOT_OWNER_PRO_STEP) {
                    //此产品未录入工序 要弹框
                    JfxUtils.showNotOwnerProcessDialog(application.getStage());
                } else if (result == ResponseBodyCode.REPEAT_SCAN) {
                    //重复扫码 需要弹框
                    JfxUtils.showRepeatScanDialog(application.getStage());
                } else if (result == ResponseBodyCode.UNCOMPLETE_PROCESS) {
                    JfxUtils.showUncompleteProcessDialog(application.getStage(), processResponseBodyApiResult.getData().getList());
                    if (consumer != null) {
                        consumer.accept(processResponseBodyApiResult.getData());
                    }
                } else {
                    LogUtils.log(TAG, processResponseBodyApiResult.getMsg());
                    JfxUtils.showLongToast(application.getStage(), processResponseBodyApiResult.getMsg());
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                throwable.printStackTrace();
                LogUtils.logE(throwable);
                jfxLoadingForm.close();
                JfxUtils.showLongToast(application.getStage(), throwable.getMessage());
            }
        });
        mDisposables.add(disposable);
    }

    /**
     * 释放资源
     */
    public void release() {
        LogUtils.log(TAG, "reset apiCreateObservableInstance");
        if (mDisposables != null) {
            for (int i = 0; i < mDisposables.size(); i++) {
                if (mDisposables.get(i) != null && !mDisposables.get(i).isDisposed()) {
                    mDisposables.get(i).dispose();
                    LogUtils.log(TAG, "CloseDisposable " + i);
                }
            }
            mDisposables.clear();
        }
        mDisposables = null;
        sApiDataService = null;
        LogUtils.log(TAG, "end CloseDisposable");
    }

}
